package com.maxus.mini.backend.open.api.controller.user;

import java.util.List;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maux.mini.data.api.redis.RedisService;
import com.maux.mini.data.api.user.UserService;
import com.maxus.mini.backend.open.api.controller.BaseController;
import com.maxus.mini.backend.open.api.vo.req.P100002Vo;
import com.maxus.mini.backend.open.api.vo.req.P100003Vo;
import com.maxus.mini.backend.open.api.vo.req.P100004Vo;
import com.maxus.mini.backend.open.api.vo.req.P100005Vo;
import com.maxus.mini.backend.open.api.vo.req.P800001Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100002Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100003Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100004Vo;
import com.maxus.mini.backend.open.api.vo.res.PR100005Vo;
import com.maxus.mini.backend.open.api.vo.res.PR800001Vo;
import com.maxus.mini.common.ElumClass.UserStatus;
import com.maxus.mini.common.utils.BeanUtilsExtended;
import com.maxus.mini.common.utils.StringUtil;
import com.maxus.mini.common.vo.ResBody;
import com.maxus.mini.entity.user.User;
import com.maxus.mini.entity.vo.user.UserVo;

/**
 * 用户controller
 * 
 * @author wujinsong
 *
 */
@RestController()
@RequestMapping(value = "/api/v1.0/user")
public class UserController extends BaseController {
	@Reference(version = "1.0.0")
	private RedisService redisService;
	@Reference(version = "1.0.0")
	public UserService<User, User> userService;
	
	/**
	 * 用户列表
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "list", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody list(@RequestBody P800001Vo req) {
		PR800001Vo retVo = new PR800001Vo();
		List<User> list = null;

		UserVo vo = new UserVo();
		try {
			BeanUtilsExtended.copyProperties(vo, req);
			list = userService.page(vo).getContent();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		retVo.setItemVos(list);

		return retVo;
	}

	/**
	 * 禁用用户
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "disable", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody disable(@RequestBody P100002Vo req) {
		PR100002Vo retVo = new PR100002Vo();

		User user = userService.find(req.getUserId());
		user.setStatus(UserStatus.禁用.name());

		userService.save(user);

		return retVo;
	}

	/**
	 * 启用用户
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "enable", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody enable(@RequestBody P100003Vo req) {
		PR100003Vo retVo = new PR100003Vo();

		User user = userService.find(req.getUserId());
		user.setStatus(UserStatus.正常.name());

		userService.save(user);

		return retVo;
	}

	/**
	 * 批量禁用
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "batchDisable", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody batchDisable(@RequestBody P100004Vo req) {
		PR100004Vo retVo = new PR100004Vo();

		List<Long> idList = StringUtil.idsToLongList(req.getUserIds());
		List<User> users = userService.findByIdIn(idList);
		for (User user : users) {
			user.setStatus(UserStatus.禁用.name());
		}
		userService.save(users);

		return retVo;
	}

	/**
	 * 批量启用
	 * 
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "batchEnable", method = { RequestMethod.POST })
	@ResponseBody()
	public ResBody batchEnable(@RequestBody P100005Vo req) {
		PR100005Vo retVo = new PR100005Vo();

		List<Long> idList = StringUtil.idsToLongList(req.getUserIds());
		List<User> users = userService.findByIdIn(idList);
		for (User user : users) {
			user.setStatus(UserStatus.正常.name());
		}
		userService.save(users);

		return retVo;
	}
}
